﻿#region

using System;
using DomainModel.Utils;

#endregion

namespace DomainModel.Domain.Bases
{
    public abstract class PersistentObject<T> : IEquatable<PersistentObject<T>>
    {
        public virtual T Id { get; private set; }

        internal PersistentObject()
        {
        }

        protected PersistentObject(T id)
        {
            Validate.NotNull(id, "Id is required");
            Id = id;
        }

        public virtual bool Equals(PersistentObject<T> other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }
            bool res = Equals(other.Id, Id);
            return res;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
            PersistentObject<T> other = obj as PersistentObject<T>;
            if (other == null)
            {
                return false;
            }
            return Equals(other);
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        public static bool operator ==(PersistentObject<T> left, PersistentObject<T> right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(PersistentObject<T> left, PersistentObject<T> right)
        {
            return !Equals(left, right);
        }
    }
}